package com.tcs.test.http.jsoup;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tcs.model.teachertest.*;
import com.tcs.server.teacher.ITeacherService;
import com.tcs.test.BaseJunit4Test;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.collections.Lists;

import javax.annotation.Resource;
import java.util.List;

public class QueryTeacher extends BaseJunit4Test {

    private final Logger logger = LoggerFactory.getLogger(QueryTeacher.class);

    @Resource(name = "teacherService")
    private ITeacherService iTeacherService;

    ObjectMapper objectMapper = new ObjectMapper();

    @Test
    public void query() {
        List<DisciplineBean> disciplineList = iTeacherService.queryAllDisciplineBeanList();
        List<YearSubjectBean> tyearSubjectBeanList = Lists.newArrayList();                      // 二级菜单
        for (DisciplineBean disciplineBean : disciplineList) {
            List<DisciplineCategoryBean> disciplineCategoryList = iTeacherService.queryDisciplineCategory(disciplineBean);
            disciplineBean.setDisciplineCategoryList(disciplineCategoryList);
            for (DisciplineCategoryBean disciplineCategoryBean : disciplineCategoryList) {
                List<YearSubjectBean> yearSubjectBeanList = iTeacherService.queryYearSubjectBean(disciplineCategoryBean);
                tyearSubjectBeanList.addAll(yearSubjectBeanList);
//                disciplineCategoryBean.setYearSubjectList(yearSubjectBeanList);
//                for (YearSubjectBean yearSubjectBean : yearSubjectBeanList) {
//                    List<TopicResultBean> topicResultBeanList = iTeacherService.queryTopicResultList(yearSubjectBean);
//                    yearSubjectBean.setTopicList(topicResultBeanList);
//                    for (TopicResultBean topicResultBean : topicResultBeanList) {
//                        List<SelectResultBean> selectResultBeanList = iTeacherService.querySelectResultList(topicResultBean);
//                        topicResultBean.setSelectResultList(selectResultBeanList);
//                    }
//                }
            }
        }
        try {
            String jsonResult = objectMapper.writeValueAsString(disciplineList);
            String yearSubjectJson = objectMapper.writeValueAsString(tyearSubjectBeanList);
            logger.info("jsonResult : {}  \n yearSubjectJson : {}", jsonResult , yearSubjectJson);
        } catch (JsonProcessingException e) {
            logger.error("", e);
        }
    }

    @Test
    public void queryNewBase() {
        this.queryData();
//        List<TopicResultBean> topicResultList = iTeacherService.queryTopicListByTopicLike();
//        try {
//            logger.info(" topicList: {} " , objectMapper.writeValueAsString(topicResultList));
//        } catch (JsonProcessingException e) {
//            logger.error("", e);
//        }

    }

    @Test
    public void updateAndQuery() {
        List<TopicResultBean> topicResultList = iTeacherService.queryTopicListByTopicLike();
        String title = "";
        TopicBean topicBean = new TopicBean();
        for (TopicResultBean topicResultBean : topicResultList) {
            title = topicResultBean.getTitle();
            if (title.indexOf("#") < 0) {
                title = title.replace("png","png#").replace("jpeg","jpeg#").replace("jpg","jpg#");
                topicBean.setId(topicResultBean.getId());
                topicBean.setTitle(title);
                try {
                    iTeacherService.updateTopicBeanTopic(topicBean);
                } catch (Exception e) {
                    logger.error("" , e);
                }
            }
        }
    }

    @Test
    public void test() {
        String test = "问号处的是（） p img-file=http://wb-sys.tpddns.cn:8012/src/e2e3b1e9f80b4327adb5067a49f9e87d.jpeg";
        int index = test.indexOf("img-file=") + 1;
        logger.info("test : {} ", test.substring(index, test.length()));
    }

    /**
     * 打印二级数据. 年度  和 题目
     */
    public void queryData() {
        List<DisciplineBean> disciplineList = iTeacherService.queryAllDisciplineBeanList();
        List<DisciplineCategoryBean> totalList = Lists.newArrayList();
        List<YearSubjectBean> yearSubjectList = Lists.newArrayList();
        List<TopicResultBean> topicResultList = Lists.newArrayList();
        for (DisciplineBean disciplineBean : disciplineList) {
            List<DisciplineCategoryBean> disciplineCategoryList = iTeacherService.queryDisciplineCategory(disciplineBean);
            totalList.addAll(disciplineCategoryList);
            for (DisciplineCategoryBean disciplineCategoryBean : disciplineCategoryList) {
                List<YearSubjectBean> yearSubjectBeanList = iTeacherService.queryYearSubjectBean(disciplineCategoryBean);
                yearSubjectList.addAll(yearSubjectBeanList);
                for (YearSubjectBean yearSubjectBean : yearSubjectBeanList) {
                    List<TopicResultBean> tempTopicList = iTeacherService.queryTopicResultList(yearSubjectBean);
                    for (TopicResultBean topicResultBean : tempTopicList) {
                        topicResultBean.setSelectResultList(iTeacherService.querySelectResultList(topicResultBean));
                    }
                    topicResultList.addAll(tempTopicList);
                    // queryTopicResultList
                }
            }
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            logger.info(" disciplineList  :  \n {} \n , totalList : \n {}" , objectMapper.writeValueAsString(disciplineList) , objectMapper.writeValueAsString(totalList));
            logger.info("map : \n {}  \n yearSubjectList : {}" , objectMapper.writeValueAsString(topicResultList) , objectMapper.writeValueAsString(yearSubjectList));
        } catch (JsonProcessingException e) {
            logger.error("" , e);
        }
    }
}
